package com.resLoader
{
	import flash.events.EventDispatcher;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;

	/**
	 * 按照配置文件加载
	 * @author hyy
	 *
	 */
	public class PreLoad extends EventDispatcher
	{
		private static var instance : PreLoad;
		public var xml : XML;
		/**
		 * 加载配置xml的加载器
		 */
		private var resource : XMLResource;
		/**
		 * 每个资源地址,版本号,id
		 */
		public var resData : Dictionary;
		/**
		 * 批量加载地址
		 */
		private var batchData : Dictionary;

		protected var returnComplete : Function;

		protected var resources : Resources;

		public function PreLoad()
		{
			resources = Resources.getInstance();
		}


		public static function getInstance() : PreLoad
		{
			if(instance == null)
			{
				instance = new PreLoad();
			}
			return instance;
		}

		/**
		 * 加载成功
		 * @param res
		 *
		 */
		public function onConfigComplete(xml:XML) : void
		{
//			var xml : XML = res.getDataByName("config.xml");
			
			this.parseConfigData(xml);
			this.parseBatchData(xml);
		}

		/**
		 * 解析加载地址
		 * @param xml
		 *
		 */
		protected function parseConfigData(data : XML) : void
		{
			this.xml = data;

//			Config.ico_url = data["icoUrl"][0].toString();
//			Config.map_url = data["mapUrl"][0].toString();
//			Config.sound_url = data["soundUrl"][0].toString();
//			Config.role_url =  data["roleUrl"][0].toString();
//			
//			Config.BODY = data["BODY"][0].toString();
//			Config.LOAD_HEAD = data["LOAD_HEAD"][0].toString();
//			Config.GAME_HEAD = data["GAME_HEAD"][0].toString();
//			Config.PET_AVATAR =  data["PET_AVATAR"][0].toString();
//			
//			Config.debug = data["debug"][0] == "true";
//			Config.feed = data["feed"][0] == "true";
//			Config.frameRate = data.frameRate[0];

			resData = new Dictionary();
			var xmlList : XMLList = data.file;

			var len : int = xmlList.length();
			var resourceData : ResData;

			for(var i : int = 0; i < len; i++)
			{
				var child : XML = xmlList[i];
				var id : String = child.@id;
				var url : String = child.@url;
				var type : String = url.split(".").pop();
				resourceData = new ResData(url, type, 0);
				resData[id] = resourceData;
			}
		}

		/**
		 * 解析批量下载id
		 *
		 */
		protected function parseBatchData(xml : XML) : void
		{
			batchData = new Dictionary();
			var batchArray : Array;
			var xmlList : XMLList = xml.batch;
			var len : int = xmlList.length();

			for(var i : int = 0; i < len; i++)
			{
				var child : XML = xmlList[i];
				//预加载的id
				var id : String = child.@id;
				var count : int = child.file.length();
				//id所对应的一系列资源地址
				batchArray = [];

				for(var j : int = 0; j < count; j++)
				{
					batchArray.push(child.file[j].@id.toString());
				}
				batchData[id] = batchArray;
			}
		}

		/**
		 * 根据id进行批量加载
		 * @param id
		 * @param callFun
		 *
		 */
		public function bathLoad(id : String, callFun : Function = null, onProgress : Function = null, context : LoaderContext = null) : void
		{
			resources.reset();
			var loads : Array = id.split(",");

			for each(var key : String in loads)
			{
				addResourceUrl(key);
			}

			resources.startLoad(callFun, onProgress, context);
		}

		/**
		 * 根据id进行后台加载
		 * @param id
		 * @param callFun
		 *
		 */
		public function preLoad(id : String, callFun : Function = null) : void
		{
			var tmpArr : Array = batchData[id];
			var len : int = tmpArr.length;
			var resourceData : ResData;

			for(var i : int = 0; i < len; i++)
			{
				var loadID : String = tmpArr[i];
				resourceData = resData[loadID];
				resourceData.addReturnFun(callFun);
				resources.preLoadResData(resourceData);
			}
		}

		/**
		 * 检测根据ID后台加载的是否完成
		 * @param id
		 * @return
		 *
		 */
		public function checkPreLoadFinish(id : String) : Boolean
		{
			var tmpArr : Array = batchData[id];
			var len : int = tmpArr.length;
			var resourceData : ResData;

			for(var i : int = 0; i < len; i++)
			{
				var loadID : String = tmpArr[i];
				resourceData = resData[loadID];

				if(!resources.isFinishLoad(resourceData.url))
				{
					return false;
				}
			}
			return true;
		}

		/**
		 * 添加资源地址
		 * @param id
		 *
		 */
		public function addResourceUrl(id : String) : void
		{
			var tmpArr : Array = batchData[id];
			var len : int = tmpArr.length;
			var resourceData : ResData;

			for(var i : int = 0; i < len; i++)
			{
				var loadID : String = tmpArr[i];
				resourceData = resData[loadID];
				resources.addResData(resourceData);
			}
		}

		public function getUrlById(id : String) : String
		{
			if(xml == null)
				return "";
			return xml.file.(@id == id).@url;
		}

		public function getResData(id : String) : ResData
		{
			return resData[id]
		}

	}
}